﻿/*
Copyright 2009 Matías Fidemraizer (www.matiasfidemraizer.com).

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

using System;
using System.Linq;
using System.Collections.Generic;

#region "Custom namespace declarations"
using Comkarl;
#endregion

namespace Comkarl.Architecture.Patterns.MVC.Entities
{
    /// <summary>
    /// Represents a list of entities for the paradigm.
    /// </summary>
    /// <typeparam name="TIdentifierName">
    /// The type for the entity identifier's name.
    /// </typeparam>
    /// <typeparam name="TEntity">
    /// The type of the list.
    /// </typeparam>
    public class EntityList<TIdentifierName, TEntity> : List<TEntity> where TEntity : Entity<TIdentifierName> where TIdentifierName : IEquatable<TIdentifierName>
    {
        #region "Fields"
        /// <summary>
        /// Retrieves a predicate which is going to validate an incoming entity going to be added in the list.
        /// </summary>
        private Predicate<TEntity> itemAddingValidator;

        /// <summary>
        /// Retrieves a predicte which is going to validate a try to get an item in the list;
        /// </summary>
        private Predicate<TEntity> itemGettingValidator;
        #endregion

        #region "Properties"
        /// <summary>
        /// Gets or sets a predicate which is going to validate an incoming entity going to be added in the list.
        /// </summary>
        public Predicate<TEntity> ItemAddingValidator
        {
            get
            {
                return itemAddingValidator;
            }
            set
            {
                itemAddingValidator = value;
            }
        }

        /// <summary>
        /// Gets or sets a predicate which is going to validate a try to get an entity in the list.
        /// </summary>
        public Predicate<TEntity> ItemGettingValidator
        {
            get
            {
                return itemGettingValidator;
            }
            set
            {
                itemGettingValidator = value;
            }
        }

        /// <summary>
        /// Hides the default indexer for forcing to get an entity by its id.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        new private TEntity this[int index] { get { return null; } }

        /// <summary>
        /// Gets an entity by its identifier.
        /// </summary>
        /// <param name="id">
        /// The identifier for an existing entity in the list.
        /// </param>
        /// <returns></returns>
        public TEntity this[Identifier<TIdentifierName> id]
        {
            get
            {
                Identifier<TIdentifierName>.Validate(id);

                // Checks if an entity for the provided identifier exists in the list.
                TEntity entityFound = (from TEntity currentEntity in this where currentEntity.ID == id select currentEntity).FirstOrDefault<TEntity>();

                if (entityFound != null)
                {
                    if (ItemGettingValidator != null)
                    {
                        ItemGettingValidator.Invoke(entityFound);
                    }

                    return entityFound;
                }
                else
                {
                    throw new EntityNotFoundException<TIdentifierName>(id, new ArgumentException(MVCException<TIdentifierName>.GetLocalizedStringByID("EntityIdArgumentNotFound"), "id"));
                }
            }
        }
        
        #endregion

        #region "Methods"
        /// <summary>
        /// Adds a new entity in the list.
        /// </summary>
        /// <param name="value">
        /// The entity instance which is going to be added.
        /// </param>
        new public void Add(TEntity value)
        {
            // If there's an item validator, its predicate is invoked.
            if (itemAddingValidator != null)
            {
                itemAddingValidator.Invoke(value);
            }

            // Adds the on-going entity to the list.
            base.Add(value);
        }
        #endregion
    }
}
